package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Transfer;
import cn.wolfcode.crm.mapper.TransferMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ITransferService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class TransferServiceImpl implements ITransferService {

    @Autowired
    private TransferMapper transferMapper;

    @Override
    public Transfer selectByPrimaryKey(Long id) {
        return transferMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Transfer> selectAll() {
        return transferMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int total = transferMapper.query4Count(qo);
        if(total == 0 ){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<?> rows = transferMapper.query4List(qo);
        return new PageResult(total,rows);
    }

    @Override
    public void insert(Transfer entity) {
        Subject subject = SecurityUtils.getSubject();
        Employee principal = (Employee) subject.getPrincipal();
        entity.setTransferDate(new Date());
        entity.setTransfer(principal);
        transferMapper.insert(entity);
    }
}
